import {CURRENT_PLAYING_MUSIC, MUSIC_PLAY_LIST} from "@/store/mutation-types";
import Vue from "vue";
import {mergeArrar} from "@/utils/util";

const musicList = {
    state: {
        currentMusic: {},
        PlayList: []
    },
    mutations: {
        SET_CURRENTMUSIC: (state, currentMusic) => {
            state.currentMusic = currentMusic
            Vue.ls.set(CURRENT_PLAYING_MUSIC,currentMusic)
        },
        SET_PLAYLIST:(state,PlayList)=>{
            state.PlayList = mergeArrar(Vue.ls.get(MUSIC_PLAY_LIST),PlayList);
            Vue.ls.set(MUSIC_PLAY_LIST, state.PlayList);
        },
        SET_MUSIC_TO_LIST:(state,music)=>{
            state.PlayList =  mergeArrar(Vue.ls.get(MUSIC_PLAY_LIST),new Array(music))
            Vue.ls.set(MUSIC_PLAY_LIST,state.PlayList)
        },
        SET_MUSIC_CLEAR:(state,array)=>{
            Vue.ls.remove(MUSIC_PLAY_LIST)
            Vue.ls.remove(CURRENT_PLAYING_MUSIC)
            state.PlayList = array
        },
    },
    actions:{
        updateCurrentMusic({commit},music){
            commit('SET_CURRENTMUSIC',music)
        },
        // 添加音乐到播放列表中
        addMusicToList({commit},music){
            commit('SET_MUSIC_TO_LIST',music)
            commit('SET_CURRENTMUSIC',music)
        },
        resetPlayList({commit},playList){ // 替换播放列表
            commit('SET_PLAYLIST',playList)
        }
    }
}
export default musicList
